home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / viewers / mpgplyr1.lha / src / motionvector.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  6KB  |  211 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include "video.h"
  22. #include "util.h"
  23.  
  24.  
  25. /*
  26.  *--------------------------------------------------------------
  27.  *
  28.  * ComputeVector --
  29.  *
  30.  *    Computes motion vector given parameters previously parsed
  31.  *      and reconstructed.
  32.  *
  33.  * Results:
  34.  *      Reconstructed motion vector info is put into recon_* parameters
  35.  *      passed to this function. Also updated previous motion vector
  36.  *      information.
  37.  *
  38.  * Side effects:
  39.  *      None.
  40.  *
  41.  *--------------------------------------------------------------
  42.  */
  43.  
  44. static void 
  45. ComputeVector(recon_right_ptr, recon_down_ptr,
  46.           recon_right_prev_ptr, recon_down_prev_ptr,
  47.           f, full_pel_vector,
  48.           motion_h_code, motion_v_code,
  49.           motion_h_r, motion_v_r)
  50.      int *recon_right_ptr, *recon_down_ptr;
  51.      int *recon_right_prev_ptr, *recon_down_prev_ptr;
  52.      unsigned int f;
  53.      BOOLEAN full_pel_vector;
  54.      int motion_h_code, motion_v_code;
  55.      unsigned int motion_h_r, motion_v_r;
  56.  
  57. {
  58.   int comp_h_r, comp_v_r;
  59.   int right_little, right_big, down_little, down_big;
  60.   int max, min, new_vector;
  61.  
  62.   /* The following procedure for the reconstruction of motion vectors 
  63.      is a direct and simple implementation of the instructions given
  64.      in the mpeg December 1991 standard draft. 
  65.   */
  66.  
  67.   if (f == 1 || motion_h_code == 0)
  68.     comp_h_r = 0;
  69.   else 
  70.     comp_h_r = f - 1 - motion_h_r;
  71.  
  72.   if (f == 1 || motion_v_code == 0)
  73.     comp_v_r = 0;
  74.   else 
  75.     comp_v_r = f - 1 - motion_v_r;
  76.  
  77.   right_little = motion_h_code * f;
  78.   if (right_little == 0)
  79.     right_big = 0;
  80.   else {
  81.     if (right_little > 0) {
  82.       right_little = right_little - comp_h_r;
  83.       right_big = right_little - 32 * f;
  84.     }
  85.     else {
  86.       right_little = right_little + comp_h_r;
  87.       right_big = right_little + 32 * f;
  88.     }
  89.   }
  90.  
  91.   down_little = motion_v_code * f;
  92.   if (down_little == 0)
  93.     down_big = 0;
  94.   else {
  95.     if (down_little > 0) {
  96.       down_little = down_little - comp_v_r;
  97.       down_big = down_little - 32 * f;
  98.     }
  99.     else {
  100.       down_little = down_little + comp_v_r;
  101.       down_big = down_little + 32 * f;
  102.     }
  103.   }
  104.   
  105.   max = 16 * f - 1;
  106.   min = -16 * f;
  107.  
  108.   new_vector = *recon_right_prev_ptr + right_little;
  109.  
  110.   if (new_vector <= max && new_vector >= min)
  111.     *recon_right_ptr = *recon_right_prev_ptr + right_little;
  112.                       /* just new_vector */
  113.   else
  114.     *recon_right_ptr = *recon_right_prev_ptr + right_big;
  115.   *recon_right_prev_ptr = *recon_right_ptr;
  116.   if (full_pel_vector)
  117.     *recon_right_ptr = *recon_right_ptr << 1;
  118.  
  119.   new_vector = *recon_down_prev_ptr + down_little;
  120.   if (new_vector <= max && new_vector >= min)
  121.     *recon_down_ptr = *recon_down_prev_ptr + down_little;
  122.                       /* just new_vector */
  123.   else
  124.     *recon_down_ptr = *recon_down_prev_ptr + down_big;
  125.   *recon_down_prev_ptr = *recon_down_ptr;
  126.   if (full_pel_vector)
  127.     *recon_down_ptr = *recon_down_ptr << 1;
  128. }
  129.  
  130.  
  131. /*
  132.  *--------------------------------------------------------------
  133.  *
  134.  * ComputeForwVector --
  135.  *
  136.  *    Computes forward motion vector by calling ComputeVector
  137.  *      with appropriate parameters.
  138.  *
  139.  * Results:
  140.  *    Reconstructed motion vector placed in recon_right_for_ptr and
  141.  *      recon_down_for_ptr.
  142.  *
  143.  * Side effects:
  144.  *      None.
  145.  *
  146.  *--------------------------------------------------------------
  147.  */
  148.  
  149. void 
  150. ComputeForwVector(recon_right_for_ptr, recon_down_for_ptr)
  151.      int *recon_right_for_ptr;
  152.      int *recon_down_for_ptr;
  153. {
  154.  
  155.   Pict *picture;
  156.   Macroblock *mblock;
  157.  
  158.   picture = &(curVidStream->picture);
  159.   mblock = &(curVidStream->mblock);
  160.  
  161.   ComputeVector(recon_right_for_ptr, recon_down_for_ptr,
  162.         &(mblock->recon_right_for_prev), 
  163.         &(mblock->recon_down_for_prev),
  164.         picture->forw_f, picture->full_pel_forw_vector,
  165.         mblock->motion_h_forw_code, mblock->motion_v_forw_code,
  166.         mblock->motion_h_forw_r, mblock->motion_v_forw_r); 
  167. }
  168.  
  169.  
  170. /*
  171.  *--------------------------------------------------------------
  172.  *
  173.  * ComputeBackVector --
  174.  *
  175.  *    Computes backward motion vector by calling ComputeVector
  176.  *      with appropriate parameters.
  177.  *
  178.  * Results:
  179.  *    Reconstructed motion vector placed in recon_right_back_ptr and
  180.  *      recon_down_back_ptr.
  181.  *
  182.  * Side effects:
  183.  *      None.
  184.  *
  185.  *--------------------------------------------------------------
  186.  */
  187.  
  188. void 
  189. ComputeBackVector(recon_right_back_ptr, recon_down_back_ptr)
  190.      int *recon_right_back_ptr;
  191.      int *recon_down_back_ptr;
  192. {
  193.   Pict *picture;
  194.   Macroblock *mblock;
  195.  
  196.   picture = &(curVidStream->picture);
  197.   mblock = &(curVidStream->mblock);
  198.  
  199.   ComputeVector(recon_right_back_ptr, recon_down_back_ptr,
  200.         &(mblock->recon_right_back_prev), 
  201.         &(mblock->recon_down_back_prev),
  202.         picture->back_f, picture->full_pel_back_vector,
  203.         mblock->motion_h_back_code, mblock->motion_v_back_code,
  204.         mblock->motion_h_back_r, mblock->motion_v_back_r); 
  205.  
  206. }
  207.  
  208.  
  209.  
  210.  
  211.